package com.example.tsproject.service.impl;

import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.PageBean;
import com.example.tsproject.entity.TeacherPosts;
import com.example.tsproject.mapper.TeacherPostsMapper;
import com.example.tsproject.service.TeacherPostsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class TeacherPostsServiceImpl implements TeacherPostsService {
    @Autowired
    private TeacherPostsMapper teacherPostsMapper;

    @Override
    public PageBean<TeacherPosts> list(Integer pageNum, Integer pageSize, String name, String content, LocalDateTime startTime, LocalDateTime endTime) {
        //查看用户是否登录
        Map<String,Object> map = ThreadLocalUtil.get();
        System.out.println(map);

        // 创建 PageBean 对象
        PageBean<TeacherPosts> pageBean = new PageBean<>();

        // 开启分页查询 PageHelper
        PageHelper.startPage(pageNum, pageSize);

        // 调用 mapper
        List<TeacherPosts> teacherPosts = teacherPostsMapper.list(name, content, startTime, endTime);
        System.out.println(teacherPosts);
        System.out.println(pageNum);
        System.out.println(pageSize);

        // 使用 PageInfo 获取分页信息
        PageInfo<TeacherPosts> pageInfo = new PageInfo<>(teacherPosts);

        // 填充 PageBean 对象
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setItems(pageInfo.getList());

        return pageBean;
    }
}
